//
//  BoatListViewController.m
//  WOFishingBoat
//
//  Created by 郑逸凯 on 2018/12/18.
//  Copyright © 2018年 郑逸凯. All rights reserved.
//

#import "BoatListViewController.h"
#import <Masonry/Masonry.h>
#import <MJRefresh/MJRefresh.h>
#import "BoatInfoCell.h"
#import "BoatInfoModel.h"
#import "BoatDetialViewController.h"
#import "WhiteNaviBar.h"
#import "YAUserDefault.h"
#import "LoginManager.h"

@interface BoatListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,assign)NSInteger currentPage;
@property (nonatomic,strong)NSMutableArray *boatListArray;
@property (nonatomic,strong)RequestSession *boatListRequest;
@property (nonatomic,strong)WhiteNaviBar *naviBar;
@property (nonatomic,strong)MBProgressHUD *loginProgressHUD;
@end

@implementation BoatListViewController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentPage = 0;
    [self.view addSubview:self.tableView];
    
    self.naviBar = [WhiteNaviBar newBar];
    [self.naviBar setTitle:@"监控截图"];
    [self.view addSubview:self.naviBar];
    
    [self performLayout];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginNotificationAction:) name:WOLoginSuccessNotification object:nil];
    @weakify(self);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        self.currentPage = 0;
        [self requestBoatList];
    }];
    if ([[LoginManager sharedInstance] hadAccountInfo]) {
        self.loginProgressHUD = [YAToast showActivityMessageInView:@"登录中..." timer:10];
    }
//    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//        @strongify(self);
//        [self requestBoatList];
//    }];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setHidden:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - Public Methods
#pragma mark - Responder Actions


- (void)loginNotificationAction:(NSNotification *)noti {
    if (self.loginProgressHUD) {
        [self.loginProgressHUD hide:YES];
        self.loginProgressHUD = nil;
    }
    [self requestBoatList];
}

#pragma mark - Delegates
#pragma mark - Tableview data source


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SCREEN_SCALE_VALUE_IN_4_7(288);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.boatListArray.count;;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BoatInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BoatInfoCell" forIndexPath:indexPath];
    [cell reloadCell:self.boatListArray[indexPath.row]];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BoatInfoModel *model = self.boatListArray[indexPath.row];
    BoatDetialViewController *vc = [[BoatDetialViewController alloc] initWithModel:model];
    [self.navigationController pushViewController:vc animated:YES];
    
}
#pragma mark - Private Methods
- (void)performLayout {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.naviBar.mas_bottom);
    }];
}

- (void)requestBoatList {
   
    @weakify(self);
    self.currentPage++;
    NSString *urlStr = WO_PATH_BOAT_LIST;
    [self.boatListRequest getWithURLString:WO_URL(urlStr) parameters:@{} completionHandler:^(id responseObject, NSError *error) {
        @strongify(self);
        if (self.tableView.mj_header.isRefreshing) {
            [self.tableView.mj_header endRefreshing];
        }
        if (self.tableView.mj_footer.isRefreshing) {
            [self.tableView.mj_footer endRefreshing];
        }
        if (!error) {
            NSDictionary *data = [responseObject tm_dictionaryForKey:@"data"];
            NSArray *listArray = [data tm_arrayForKey:@"shipList"];
            NSMutableArray *array = [NSMutableArray array];
            for (NSDictionary *boatDic in listArray) {
                [array addObject:[BoatInfoModel yy_modelWithDictionary:boatDic]];
            }
            if (self.currentPage == 1) {
                self.boatListArray = array;
            }else if(array.count > 0){
                [self.boatListArray addObjectsFromArray:array];
            }
            [self.tableView reloadData];
        }else {
            self.currentPage--;
            [YAToast showTouchableTipMessageInView:error.domain];
        }
    }];
}

#pragma mark - Setters and Getters
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;

        [_tableView registerClass:[BoatInfoCell class] forCellReuseIdentifier:@"BoatInfoCell"];
    }
    return _tableView;
}

- (RequestSession *)boatListRequest {
    if (!_boatListRequest) {
        _boatListRequest = [[RequestSession alloc] init];
    }
    return _boatListRequest;
}


#pragma mark - Helper


@end
